﻿#include "windowsproxy.h"
#include <QThread>
#include <QDebug>

WindowsProxy::WindowsProxy(QObject *parent) : QObject(parent)
{
    QString json = openConfig();
    
    proxy = new ClientCorrespond();
    QThread *thread = new QThread(this);
    proxy->moveToThread(thread);
    
    //连接服务器信号
    
    connect(this,&WindowsProxy::signal_ConnectToServer,proxy,&ClientCorrespond::connectToServer,Qt::QueuedConnection);
    connect(this,&WindowsProxy::signal_OpenPort,proxy,&ClientCorrespond::openPort,Qt::QueuedConnection);
    connect(this,&WindowsProxy::signal_dissconnectFromServer,proxy,&ClientCorrespond::stopServer,Qt::QueuedConnection);
    connect(this,&WindowsProxy::signal_ClosePort,proxy,&ClientCorrespond::closePort,Qt::QueuedConnection);
    //连接状态更改
    connect(proxy,&ClientCorrespond::connectStateChange,this,&WindowsProxy::connectStatusChange);
    connect(proxy,&ClientCorrespond::forwardStatusChange,this,&WindowsProxy::forwardStatusChange);
    
    thread->start();
    
}

void WindowsProxy::connectToServer(QString ip)
{
    Forward_Config::SetServerHost(ip);
    emit signal_ConnectToServer();
}

void WindowsProxy::dissconnectFromServer()
{
    emit signal_dissconnectFromServer();
}

QString WindowsProxy::openConfig()
{
    QFile file("./config/client.config");
    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug()<<"打开配置文件失败";
        return "";
    }
    QString json = file.readAll();
    file.close();
    
    qDebug()<<"配置文件";
    qDebug()<<json;
    
    emit signal_OpenConfig(json);
    return json;
}

void WindowsProxy::openForward(QString json)
{
    qDebug()<<"开始转发 "<<json;
    
    QJsonObject object;
    if(!CommandTools::StirngTojson(json.toUtf8(),object))
    {
        //不成功，发送错误消息
        return;
    }
    QByteArray app_ip = object["appHost"].toString().toUtf8();
    
    QJsonObject info;
    info["appHost"] = app_ip.data();
    info["forwardHost"] = Forward_Config::GetServerHost();
    
    info["appPort"] = object["appPort"].toString();
    info["forwardPort"] = object["forwardPort"].toString();
    info["forwardTimeout"] = Forward_Config::GetTimeoutSecond();
    info["appTimeout"] = Forward_Config::GetTimeoutSecond();
    emit signal_OpenPort(info);
}

void WindowsProxy::stopForward(QString json)
{
    QJsonObject object;
    if(!CommandTools::StirngTojson(json.toUtf8(),object))
    {
        //不成功，发送错误消息
        return;
    }
    
    QByteArray app_ip = object["appip"].toString().toUtf8();
    
    QJsonObject info;
    info["appHost"] = app_ip.data();
    info["forwardHost"] = Forward_Config::GetServerHost();
    
    info["appPort"] = object["localport"].toInt();
    info["forwardPort"] = object["forwardPort"].toInt();
    info["forwardTimeout"] = Forward_Config::GetTimeoutSecond();
    info["appTimeout"] = Forward_Config::GetTimeoutSecond();
    
    emit signal_ClosePort(info);
}

void WindowsProxy::connectStatusChange(CONNECT_STATUS status)
{
    emit signal_ConnectStatusChange(status);
}

void WindowsProxy::forwardStatusChange(int port, CONNECT_STATUS status)
{
    emit signal_ForwardStatusChange(port,status);
}
